#include <stdio.h>
#include <malloc.h>
#include <string.h>

#include "string_utils.h"

int main()
{
    char s1[64] = {'\0'}; 
    char s2[64] = {'\0'};
    char s3[64] = {'\0'};
    char str1[] = "  This is a test.    ";
    char str2[] = "  这是一个测试.      ";
    char str3[] = "abobo abobo abobo asdfa abobo adsf";
    char str4[] = "abobo";

    printf("\ntest the intCounts\n");
    printf("the abobo counts:%d\n", countWords(str3, str4));
    
    strncpy(s1, str1, strlen(str1));
    strncpy(s2, str1, strlen(str1));
    strncpy(s3, str2, strlen(str2));


    printf("s1[] = SOL%sEOL\n", s1);
    printf("s2[] = SOL%sEOL\n", s2);
    printf("s3[] = SOL%sEOL\n", s3);
    printf("strlen(str1) = %d,strlen(str2) = %d\n", strlen(str1), strlen(str2));

    char *p1 = (char *)calloc(strlen(s1), sizeof(char));
    char *p2 = (char *)calloc(strlen(s2), sizeof(char));
    char *p3 = (char *)calloc(strlen(s3), sizeof(char));

    printf("/*============= ltrim test ==============*/\n");
    strncpy(p1, ltrim(s1), strlen(s1));
    strncpy(p2, s2, strlen(s2));
    strncpy(p3, s3, strlen(s3));

#if 0
    printf("after strncpy(p2, s2, strlen(s2): p2 = SOL%sEOL\n", p2);
    printf("after strncpy(p3, s3, strlen(s3): p3 = SOL%sEOL\n", p3);
#endif
 
    printf("ltrim(s1) = SOL%sEOL\n", p1);
    printf("ltrim(p2) = SOL%sEOL, p2 = SOL%sEOL\n", ltrim(p2), p2);
    printf("ltrim(p3) = SOL%sEOL, p3 = SOL%sEOL\n", ltrim(p3), p3);

    if (p1)
    {
        free(p1);
        p1 = NULL;
    }
    if(p2)
    {
        free(p2); 
        p2 = NULL;
    }
    if(p3)
    {
        free(p3); 
        p3 = NULL;
    }
    
    strncpy(s1, str1, strlen(str1));
    strncpy(s2, str1, strlen(str1));
    strncpy(s3, str2, strlen(str2));
    p1 = (char *)calloc(strlen(s1), sizeof(char));
    p2 = (char *)calloc(strlen(s2), sizeof(char));
    p3 = (char *)calloc(strlen(s3), sizeof(char));


    printf("/*============= rtrim test ==============*/\n");
    strncpy(p1, rtrim(s1), strlen(s1));
    strncpy(p2, s2, strlen(s2));
    strncpy(p3, s3, strlen(s3));
    
    printf("rtrim(s1) = SOL%sEOL\n", p1);
    printf("rtrim(p2) = SOL%sEOL, p2 = SOL%sEOL\n", rtrim(p2), p2);
    printf("rtrim(p3) = SOL%sEOL, p3 = SOL%sEOL\n", rtrim(p3), p3);

    if (p1)
    {
        free(p1);
        p1 = NULL;
    }
    if(p2)
    {
        free(p2); 
        p2 = NULL;
    }
    if(p3)
    {
        free(p3); 
        p3 = NULL;
    }
    
    strncpy(s1, str1, strlen(str1));
    strncpy(s2, str1, strlen(str1));
    strncpy(s3, str2, strlen(str2));
    p1 = (char *)calloc(strlen(s1), sizeof(char));
    p2 = (char *)calloc(strlen(s2), sizeof(char));
    p3 = (char *)calloc(strlen(s3), sizeof(char));

    printf("/*============= trim test ==============*/\n");
    strncpy(p1, trim(s1), strlen(s1));
    strncpy(p2, s2, strlen(s2));
    strncpy(p3, s3, strlen(s3));
    
    printf("trim(s1) = SOL%sEOL\n", p1);
    printf("trim(p2) = SOL%sEOL, p2 = SOL%sEOL\n", trim(p2), p2);
    printf("trim(p3) = SOL%sEOL, p3 = SOL%sEOL\n", trim(p3), p3);

    if (p1)
    {
        free(p1);
        p1 = NULL;
    }
    if(p2)
    {
        free(p2); 
        p2 = NULL;
    }
    if(p3)
    {
        free(p3); 
        p3 = NULL;
    }
    
    strncpy(s1, str1, strlen(str1));
    strncpy(s2, str1, strlen(str1));
    p1 = (char *)calloc(strlen(s1), sizeof(char));
    p2 = (char *)calloc(strlen(s2), sizeof(char));

    printf("/*============= replaceChar test ==============*/\n");
    
    char cOld = 's';
    char cNew = 'S';
    strncpy(p1, replaceChar(s1, cOld, cNew), strlen(s1));
    strncpy(p2, s2, strlen(s2));

    printf("replaceChar(s1, cOld, cNew) = %s\n", p1);
    printf("replaceChar(p2, cOld, cNew) = %s, p2 = %s\n", replaceChar(p2, cOld, cNew), p2);

    if (p1)
    {
        free(p1);
        p1 = NULL;
    }
    if(p2)
    {
        free(p2); 
        p2 = NULL;
    }
    
    strncpy(s1, str1, strlen(str1));
    strncpy(s2, str1, strlen(str1));
    strncpy(s3, str2, strlen(str2));
    p1 = (char *)calloc(strlen(s1), sizeof(char));
    p2 = (char *)calloc(strlen(s2), sizeof(char));
    p3 = (char *)calloc(strlen(s3), sizeof(char));

    printf("/*============= replaceString test ==============*/\n");
    const char *pcOld = "is";
    const char *pcNew = "ISIS"; 
    
    const char *pcOldWide = "是";
    const char *pcNewWide = "是地";

    strncpy(p2, s2, strlen(s2));
    strncpy(p3, s3, strlen(s3));

    printf("replaceString(&p2, pcOld, pcNew) = %s, p2 = %s\n", replaceString(&p2, pcOld, pcNew), p2);
    printf("replaceString(&p3, pcOldWide, pcNewWide) = %s, p3 = %s\n", replaceString(&p3, pcOldWide, pcNewWide), p3);

    if (p1)
    {
        free(p1);
        p1 = NULL;
    }
    if(p2)
    {
        free(p2); 
        p2 = NULL;
    }
    if(p3)
    {
        free(p3); 
        p3 = NULL;
    }

    /*============= handleEscape test ==============*/
    /*TODO 从文件中读取带有转义字符的字符串进行测试 */
    /*============= handle0Escape test ==============*/

    /*============= hex2char test ==============*/
    printf("hex2char('6F') = %c\n", hex2char("6F"));

    return 0;
}
